//
//  AnimationViewController.m
//  UIComprehensive
//
//  Created by lanou3g on 16/1/29.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "AnimationViewController.h"
#import "ModalViewController.h"
@interface AnimationViewController ()
@property (weak, nonatomic) IBOutlet UIView *animationV;

@end

@implementation AnimationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - UIView的动画形式
- (IBAction)UIViewAnimation:(UIButton *)sender {
    //开始动画
    [UIView beginAnimations:@"标识符1" context:nil];
    //动画持续时间
    [UIView setAnimationDuration:0.5];
    //动画执行次数
    [UIView setAnimationRepeatCount:3];
    //动画的改变内容
    self.animationV.frame = CGRectMake(160, 100, 10, 10);
    //颜色渐变
    self.animationV.backgroundColor = [UIColor purpleColor];
    //代理（动画）执行者
    [UIView setAnimationDelegate:self];
    //结束动画时调的方法
    [UIView setAnimationDidStopSelector:@selector(stopAction)];
    
}
//结束动画时调的方法
- (void)stopAction{
    self.animationV.frame = CGRectMake(10, 10, 300, 280);
}
#pragma mark - UIView的动画的block形式
- (IBAction)UIVIiewBlock:(UIButton *)sender {
    [UIView animateWithDuration:0.5 animations:^{
        //动画内容
        self.animationV.frame = CGRectMake(10, 10, 10, 10);
        self.animationV.center = CGPointMake(300, 10);
    } completion:^(BOOL finished) {
        //
        self.animationV.frame = CGRectMake(10, 10, 300, 280);
    }];
}
#pragma mark - 模态
- (IBAction)transtion:(id)sender {
    
    ModalViewController *modal = [ModalViewController alloc];
    [UIView transitionWithView:self.view.superview duration:1 options:(UIViewAnimationOptionCurveLinear) animations:^{
        //动画内容：将第二个视图座位子视图
        [self.view addSubview:modal.view];
    } completion:^(BOOL finished) {
        //完成动画时跳转到第二个界面
        [self addChildViewController:modal];
    }];
    
}
#pragma mark -仿射变换:知识作变换而已，放在动画里才能实现动画
- (IBAction)CGAffinTransform:(id)sender {
    
    //UIView的transform属性
    [UIView animateWithDuration:1 animations:^{
        
        self.animationV.transform = CGAffineTransformMakeScale(0.5, 0.5);
        self.animationV.transform = CGAffineTransformTranslate(self.animationV.transform, 100, 100);
        self.animationV.transform = CGAffineTransformMakeRotation(10);
    }];
    
}
#pragma mark -CALayer层上的动话
- (IBAction)CALayer:(id)sender {
    
    //必须写position，这是CALayer的中心点
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    basicAnimation.duration = 3 ;
    basicAnimation.repeatCount = 20;
    //设置初始位置，把点转换成NSValue形式
    basicAnimation.fromValue = [NSValue valueWithCGPoint:self.animationV.layer.position];
    //layer动画的目的位置
    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(160, 400)];
    //把动画添加到layer上，后面key只是一个标识
    [self.animationV.layer addAnimation:basicAnimation forKey:@"标识符3"];
    
}
#pragma mark - keyFrame动画
- (IBAction)KeyFrame:(id)sender {
    
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
    CGPoint point = self.animationV.layer.position;
    CGFloat left = point.x - 100;
    CGFloat right = point.x + 100;
    CGFloat center = point.x;
    
    NSNumber *l = [NSNumber numberWithFloat:left];
    NSNumber *r = [NSNumber numberWithFloat:right];
    NSNumber *cen = [NSNumber numberWithFloat:center];
    
    //震动范围
    keyFrameAnimation.values = @[l,cen,r,cen,l,cen,r,l,cen,r,l,cen,r,l,cen,r,l,cen,r];
    keyFrameAnimation.duration = 1;
    
    [self.animationV.layer addAnimation:keyFrameAnimation forKey:@"标识符4"];
}
#pragma mark - group
- (IBAction)Group:(id)sender {
    
    //将动画放在分组里
    CAAnimationGroup *group = [CAAnimationGroup animation];
    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position"];
    basic.fromValue = [NSValue valueWithCGPoint:self.animationV.layer.position];
    basic.toValue = [NSValue valueWithCGPoint:CGPointMake(250, 300)];
    
    CABasicAnimation *basic1 = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    basic1.fromValue = (__bridge id _Nullable)([UIColor cyanColor].CGColor);
    basic1.toValue = (__bridge id _Nullable)([UIColor greenColor].CGColor);
    group.animations = @[basic,basic1];
    [self.animationV.layer addAnimation:group forKey:@"group"];
}
#pragma mark -layerTransform(3D效果)
- (IBAction)LayerTransform:(id)sender {
    
    CATransition *animation = [CATransition animation];
    animation.duration = 3;
    animation.repeatCount = 100;
    animation.type = @"rippleEffect";
    animation.subtype = kCATransitionFromLeft;
    [self.view.layer addAnimation:animation forKey:@"标识符5"];
}

@end
